<?php

namespace app\admin\controller;
use think\Controller;
use think\image\Exception;

class Menu extends Common {

    private $obj;
    public function _initialize()
    {
        $this->obj = model("menu");
    }

    public function index(){
        $data = array();
        if (isset($_REQUEST['type']) && in_array($_REQUEST['type'], array(0,1))) {
            $data['type'] = intval($_REQUEST['type']);
            $this->assign('type',$data['type']);
        }else {
            $this->assign('type',-100);
        }
        /*
         * 分页操作
         */
        $menuList = $this->obj->getMenus($data);
    	return $this->fetch('',[
            'menuList' => $menuList
        ]);
    }

    public function add(){
        if (input('post.')) {
            if (!$_POST['name'] || !isset($_POST['name'] )){
                show(0, '菜单名不能为空');
            }
            if (!$_POST['m'] || !isset($_POST['m'] )){
                show(0, '模块名不能为空');
            }
            if (!$_POST['c'] || !isset($_POST['c'] )){
                show(0, '控制器不能为空');
            }
            if (!$_POST['f'] || !isset($_POST['f'] )){
                show(0, '方法名不能为空');
            }

            if(isset($_POST['menu_id']) && $_POST['menu_id']) {
                return $this->save($_POST);
            }

            $menuId = $this->obj->insert($_POST);
            if ($menuId) {
                return show(1, '新增成功', $menuId);
            }
            return show(0, '新增失败', $menuId);
        } else {
            return $this->fetch();
        }
    }

    public function edit(){
        $menuId = input('get.id');
        $ret = $this->obj->find($menuId);
        return $this->fetch('',[
            'ret'=> $ret
        ]);
    }

    public function save($data){
        $menuId = $data['menu_id'];
        unset($data['menu_id']);
        try {
            $id = $this->obj->updateMenuById($menuId, $data);
            if ($id === false) {
                return show(0, '更新失败');
            }
            return show(1, '更新成功');
        } catch (Exception $e){
            return show(0, $e->getMessage());
        }

    }

    public function setStatus(){
        if($_POST) {
            $id = $_POST['id'];
            $status = $_POST['status'];
            //执行数据更新操作
            $ret = $this->obj->updateStatusById($id, $status);
            try{
                if ($ret) {
                    return show(1, '操作成功');
                } else {
                    return show(0, '操作失败');
                }
            } catch (Exception $e) {
                return show(0, $e->getMessage());
            }
        }
        return show(0, '没有提交数据');
    }

    public function listorder(){
        $listorder = $_POST['listorder'];
        $jumpUrl = $_SERVER['HTTP_REFERER'];
        $errors = array();
        if ($listorder) {
            try {
                foreach ($listorder as $menuId => $v) {
                    // 执行更新
                    $id = $this->obj->updateMenuListorderById($menuId, $v);
                    if ($id === false) {
                        $errors[] = $menuId;
                    }
                }
            }catch (Exception $e) {
                return show(0, $e->getMessage(),array('jump_url' => $jumpUrl));
            }
            if ($errors) {
                return show(0, '排序失败-'.implode(',', $errors),array('jump_url' => $jumpUrl));
            }
            return show(1, '排序成功',array('jump_url' => $jumpUrl));
        }
        return show(0, '排序数据失败',array('jump_url' => $jumpUrl));
    }
}